package com.pinc.cpbl;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

public class game_content extends WidgetActivity {
	private WebView wv_score;
	private WebView wv_textbr;
	private SharedPreferences settings;
	private SharedPreferences.Editor editor;
	private Button b_go2top;
	private Button b_go2bot;
	private Button b_host;
	private Button b_guest;
	private Button b_textbr;
	private int game_nr;
	private byte game_status;
	private int mLeague;
	private String mURL_online;
	private String mURL_postfix;
	private static final String[] LEAGUE_GAMEKIND= {"01", "04"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.game_content);
		mLeague = 0;
		game_nr = this.getIntent().getExtras().getInt("game_nr");
		game_status = this.getIntent().getExtras().getByte("game_status", Game_Info.ST_NONE);
		mLeague = this.getIntent().getExtras().getInt("league", 0);

		settings = PreferenceManager.getDefaultSharedPreferences(this);
		editor = settings.edit();

		mURL_online = "http://online.cpbl.com.tw/online2010";
		mURL_postfix = String.format("gameno=%s&pbyear=%d&game=%d", LEAGUE_GAMEKIND[mLeague], comn_param.CPBL_YEAR, game_nr);

		initWidget();

		b_go2top.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wv_textbr.pageUp(true);
			}
		});

		b_go2bot.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wv_textbr.pageDown(true);
			}
		});

		b_host.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadAD();
				wv_textbr.loadUrl(String.format("%s/Player_Host.aspx?%s", mURL_online, mURL_postfix));
			}
		});

		b_guest.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadAD();
				wv_textbr.loadUrl(String.format("%s/Player_Guest.aspx?%s", mURL_online, mURL_postfix));
			}
		});

		b_textbr.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadAD();
				wv_textbr.loadUrl(String.format("%s/Game.aspx?%s", mURL_online, mURL_postfix));
			}
		});

		wv_score.setBackgroundColor(Color.TRANSPARENT);
		wv_textbr.setBackgroundColor(Color.TRANSPARENT);
		wv_textbr.setWebChromeClient(new myChromeClient());

		if (isNoInternet())
			return;

		wv_score.loadUrl(String.format("%s/ScoreBoard.aspx?%s", mURL_online, mURL_postfix));
		wv_textbr.loadUrl(String.format("%s/Game.aspx?%s", mURL_online, mURL_postfix));
	}

	public class myChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress >= 100)
				loadAD();
		}
	}

	@Override
	protected void initUpbar() {
		tv_title.setText(getResources().getString(R.string.str_game_content));

		if ( game_status == Game_Info.ST_LIVE || game_status == Game_Info.ST_LINEUP)
		{
			b_right2.setVisibility(View.VISIBLE);
			b_right2.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showDialog(0);
				}
			});
		}

		b_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wv_score.reload();
				wv_textbr.clearView();
				wv_textbr.pageUp(true);
				wv_textbr.reload();
			}
		});
	}

	@Override
	protected void initWidget() {
		super.initWidget();
		wv_score = (WebView) findViewById(R.id.wv_score);
		wv_textbr = (WebView) findViewById(R.id.wv_textbr);
		b_go2top = (Button) this.findViewById(R.id.button_top);
		b_go2bot = (Button) this.findViewById(R.id.button_bottom);
		b_textbr = (Button) this.findViewById(R.id.button_b1);
		b_host = (Button) this.findViewById(R.id.button_b2);
		b_guest = (Button) this.findViewById(R.id.button_b3);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		if ( settings.getInt(PreferenceHelper.BROADCAST_GAME_NR, 0) == game_nr)
			((AlertDialog)dialog).setMessage(getResources().getString(R.string.str_ask_cancel_broadcast));
		else
			((AlertDialog)dialog).setMessage(getResources().getString(R.string.str_ask_broadcast));
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(game_content.this);

		builder.setMessage("").setCancelable(false)
		.setPositiveButton(getResources().getString(R.string.str_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				if ( settings.getInt(PreferenceHelper.BROADCAST_GAME_NR, 0) == game_nr)
					editor.putInt(PreferenceHelper.BROADCAST_GAME_NR, 0);
				else
					editor.putInt(PreferenceHelper.BROADCAST_GAME_NR, game_nr);
				editor.commit();
				dialog.cancel();
				Intent i = new Intent(getApplicationContext(), BroadcastService.class);
				startService(i);
			}
		})
		.setNegativeButton(getResources().getString(R.string.str_no), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});

		return builder.create();
	}
}
;